package com.ebrothers.isns.controller;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ebrothers.isns.NavigationManager;
import com.ebrothers.isns.NavigationManager.NavigatedView;
import com.ebrothers.isns.R;
import com.ebrothers.isns.common.Constants;
import com.ebrothers.isns.data.QsRetrieveSchedule;
import com.ebrothers.isns.data.QsRetrieveSchedule.OnQsRetrievedListener;
import com.ebrothers.isns.vo.Question;

public class QsViewController extends ViewController {
	private static final String TAG = "QsViewController";
	private QsListAdapter mAdapter;
	private ArrayList<Question> mQuestions;
	private QsRetrieveSchedule mSchedule;

	public QsViewController(Context context, View view) {
		super(context, view);
	}

	@Override
	public void onViewBinded(View view) {
		if (Constants.LOGD) {
			Log.d(TAG, "****** onViewBinded. ");
		}
		mQuestions = new ArrayList<Question>();
		final ListView listQs = (ListView) view;
		AnimationSet set = new AnimationSet(true);

		Animation animation = new AlphaAnimation(0.0f, 1.0f);
		animation.setDuration(50);
		set.addAnimation(animation);

		animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
				-1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
		animation.setDuration(100);
		set.addAnimation(animation);

		LayoutAnimationController controller = new LayoutAnimationController(
				set, 0.5f);
		listQs.setLayoutAnimation(controller);
		mAdapter = new QsListAdapter();
		listQs.setAdapter(mAdapter);
		listQs.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				final Bundle data = new Bundle();
				data.putSerializable("q", mQuestions.get(position));
				NavigationManager.getInstance().navigateTo(
						NavigatedView.VIEW_AS, data, true);
			}
		});
		mSchedule = new QsRetrieveSchedule();
		mSchedule.setOnQsRetrievedListener(new OnQsRetrievedListener() {
			public void onQsRetrieved(ArrayList<Question> newQs) {
				if (!newQs.isEmpty()) {
					mQuestions.addAll(0, newQs);
					mAdapter.notifyDataSetChanged();
				}
				if (Constants.LOGD) {
					Log.d(TAG,
							"****** onQsRetrieved. mQuestions="
									+ mQuestions.size() + ", this=" + this);
				}
			}
		});
		mSchedule.execute();
	}

	@Override
	public void onViewSelected(Bundle data) {
		if (Constants.LOGD) {
			Log.d(TAG, "****** onViewSelected. ");
		}
		mSchedule.retrieveImmediately();
		onResume();
	}

	@Override
	public void onViewUnselected() {
		if (Constants.LOGD) {
			Log.d(TAG, "****** onViewUnselected. ");
		}
		onPause();
	}

	@Override
	public void onResume() {
		if (Constants.LOGD) {
			Log.d(TAG, "****** onResume. mQuestions=" + mQuestions);
		}
		if (mSchedule != null && mSchedule.isPaused()) {
			mSchedule.resume();
		}
		super.onResume();
	}

	@Override
	public void onPause() {
		if (Constants.LOGD) {
			Log.d(TAG, "****** onPause. ");
		}
		if (mSchedule != null && mSchedule.isRunning()) {
			mSchedule.puase();
		}
		super.onPause();
	}

	@Override
	public void onDestroy() {
		if (Constants.LOGD) {
			Log.d(TAG, "****** onDestroy. ");
		}
		if (mSchedule != null) {
			mSchedule.destory();
			mSchedule = null;
		}
		super.onDestroy();
	}

	class QsListAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public QsListAdapter() {
			mInflater = LayoutInflater.from(getContext());
		}

		public int getCount() {
			return mQuestions.size();
		}

		public Object getItem(int position) {
			return mQuestions.get(position);
		}

		public long getItemId(int position) {
			return mQuestions.get(position).id;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			QsViewHolder holder = null;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.isns_qs_item, null);
				holder = new QsViewHolder();
				holder.ownerHead = (ImageView) convertView
						.findViewById(R.id.owner_head);
				holder.triggerLocation = (TextView) convertView
						.findViewById(R.id.owner_location);
				holder.question = (TextView) convertView
						.findViewById(R.id.question);
				holder.answerCount = (TextView) convertView
						.findViewById(R.id.answer_count);
				convertView.setTag(holder);
			} else {
				holder = (QsViewHolder) convertView.getTag();
			}

			final Question q = mQuestions.get(position);
			holder.triggerLocation.setText(q.triggerLocation.name);
			holder.question.setText(q.body);
			int count = q.answerCount;
			String label = " answers";
			if (count <= 1) {
				label = " answer";
			}
			holder.answerCount.setText(count + label);
			return convertView;
		}
	}

	class QsViewHolder {
		ImageView ownerHead;
		TextView triggerLocation;
		TextView question;
		TextView answerCount;
	}

}
